﻿/***********************************************************************
 * <copyright file="AuthController.cs" company="BSD JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Sunday, April 13, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Web.Mvc;
using System.Web.Security;
using DXWebApplication.Areas.Auth.Models;
using DXWebApplication.Code;
using DXWebApplication.Filters;
using DXWebApplication.Repositories.Core;
using DXWebApplication.Repositories.Implementations;
using DXWebApplication.SecurityServiceReference;

namespace DXWebApplication.Areas.Auth.Controllers
{
    [AllowAnonymous]
    [InitializeSimpleMembership]
    public class AuthController : Controller
    {
        private readonly IAuthRepository _authRepository;
        /// <summary>
        /// Default Constructor for AuthController.
        /// </summary>
        public AuthController() :
            this(new AuthRepository())
        {                    }

        /// <summary>
        /// Overloaded 'injectable' Constructor for AuthController.
        ///
        /// Pattern: Constructor Dependency Injection (DI).
        /// </summary>
        /// <param name="authRepository">The authentication repository</param>
        public AuthController(IAuthRepository authRepository)
        {
            _authRepository = authRepository;
        }        

        /// <summary>
        /// Logins this instance.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// Logins the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            var message = string.Empty;            
            if (ModelState.IsValid)
            {
                var response = _authRepository.Login(model.UserName, model.Password);
                if (response.Acknowledge.Equals(AcknowledgeType.Success))
                {
                    Session["UserId"] = response.UserId;                    
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    if (!string.IsNullOrEmpty(returnUrl))
                        return Redirect(returnUrl);
                    return RedirectToAction("Index", "Admin", new { area = "Administrators" });
                }
                message = response.Message;
            }
            ModelState.AddModelError("", message);
            return View(model);
        }

        /// <summary>
        /// Logouts this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {
            _authRepository.Logout();
            FormsAuthentication.SignOut();
            return Redirect(UrlMaker.ToLogin());
        }

    }
}
